package info.androidhive.pratap;

import info.androidhive.pratap.models.Product;
import info.androidhive.volleyexamples.R;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ProductsAdapter extends BaseAdapter {

	Context context;

	List<Product> products;

	public ProductsAdapter(Context context1, List<Product> products1) {
		this.context = context1;
		this.products = products1;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return products.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return products.indexOf(getItem(position));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ProductViewHolder holder = null;

		LayoutInflater rowInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {
			convertView = rowInflater.inflate(
					R.layout.product_listview_row_item, null);

			holder = new ProductViewHolder();
			holder.tvpid = (TextView) convertView.findViewById(R.id.tvPid);
			holder.tvpname = (TextView) convertView.findViewById(R.id.tvPName);
			holder.tvprice1 = (TextView) convertView
					.findViewById(R.id.tvPrice1);

			convertView.setTag(holder);

		} else {
			holder = (ProductViewHolder) convertView.getTag();
		}

		Product item = (Product) getItem(position);

		holder.tvpname.setText(item.getPname());
		holder.tvprice1.setText("$ "+Float.toString((item.getPrice1())));

		holder.tvpid.setText(Integer.toString(item.getPid()));

		return convertView;
	}

	public class ProductViewHolder {

		TextView tvpname;

		TextView tvprice1;

		TextView tvpid;

	}

}
